package com.example.farmeri.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.farmeri.R;
import com.example.farmeri.data.config.ServiceConfig;
import com.example.farmeri.ui.farm_manager.model.ManagerModel;

import java.util.List;

/**
 * Create by weiqishan on 2020/4/6.
 * 配置适配器
 */
public class FarmManagerRecycleViewAdapter extends RecyclerView.Adapter{

    Context context;
    Integer screeWidth;
    Integer layout;
    List<ManagerModel> userModelList;
    mOnExtraClickListener onExtraClickListener;

    public FarmManagerRecycleViewAdapter(Context context, int layout, int screeWidth, List<ManagerModel> list) {
        this.context = context;
        this.layout = layout;
        this.screeWidth = screeWidth;
        this.userModelList = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = inflater.inflate(layout,viewGroup,false);
        return new MyViewHolder(itemView);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
        myViewHolder.container.scrollTo(0,0);
        myViewHolder.checkBox.setChecked(false);
        ManagerModel.User user = userModelList.get(myViewHolder.getAdapterPosition()).getUser();
        myViewHolder.managerIdentity.setText(user.getIdentity());
        if (!TextUtils.isEmpty(user.getIcon())){
            Glide.with(context).load(ServiceConfig.getRootPath(user.getIcon())).into(myViewHolder.headImg);
        }
        if ("温度管理员".equals(user.getIdentity())){
            myViewHolder.identImg.setImageDrawable(context.getDrawable(R.drawable.ic_temperature));
        }else if ("湿度管理员".equals(user.getIdentity())){
            myViewHolder.identImg.setImageDrawable(context.getDrawable(R.drawable.ic_humidity));
        }else if ("光照管理员".equals(user.getIdentity())){
            myViewHolder.identImg.setImageDrawable(context.getDrawable(R.drawable.ic_sun));
        }

        myViewHolder.managerNickName.setText(user.getNick_name());
        myViewHolder.layoutShow.setMinWidth(screeWidth);
        //设置滑动效果
        myViewHolder.container.setOnTouchListener((v, event) -> {
            //用户行为
            int action = event.getActionMasked();
            switch(action){
                case MotionEvent.ACTION_UP:
                    //操作界面的宽度
                    int hideLayoutWidth = myViewHolder.layoutHide.getWidth();
                    //滑动的距离
                    int scrollX = myViewHolder.container.getScrollX();
                    //滑动距离大于操作界面的一半时显示操作界面
                    if (scrollX > hideLayoutWidth/2){
                        myViewHolder.container.smoothScrollTo(hideLayoutWidth,0);
                    }else {
                        myViewHolder.container.smoothScrollTo(0,0);
                    }
                    return true;
            }
            return false;
        });
        myViewHolder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            //获取当前的item位置
            int position = myViewHolder.getAdapterPosition();
            if (isChecked){
                onExtraClickListener.onChecked(position,user.getId());
            }else {
                onExtraClickListener.onUnChecked(position,user.getId());
            }

        });
        //设置点击界面勾选
        myViewHolder.layoutShow.setOnClickListener(v -> {
            if (myViewHolder.container.getScrollX() > 0){
                myViewHolder.container.smoothScrollTo(0,0);
                return;
            }
            myViewHolder.checkBox.setChecked(!myViewHolder.checkBox.isChecked());
        });
        //点击删除
        myViewHolder.del.setOnClickListener(v -> {
            if (onExtraClickListener != null) {
                onExtraClickListener.onDelClick(v,myViewHolder.getAdapterPosition(),user.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return userModelList.size();
    }

    public void setOnExtraClickListener(mOnExtraClickListener onExtraClickListener) {
        this.onExtraClickListener = onExtraClickListener;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        HorizontalScrollView container; //显示界面与操作界面的容器
        ConstraintLayout layoutShow;    //显示界面
        LinearLayout layoutHide;    //操作界面
        CheckBox checkBox;    //复选框
        TextView del;
        TextView managerIdentity;   //管理员名称
        TextView managerNickName;   //管理员名称
        ImageView headImg;
        ImageView identImg;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            container = itemView.findViewById(R.id.item_farm_manager_container);
            layoutShow = itemView.findViewById(R.id.item_farm_manager_layout_show);
            layoutHide = itemView.findViewById(R.id.item_farm_manager_layout_hide);
            checkBox = itemView.findViewById(R.id.item_farm_manager_checkBox);
            del = itemView.findViewById(R.id.item_farm_manager_del);
            managerIdentity = itemView.findViewById(R.id.item_farm_manager_manager);
            managerNickName = itemView.findViewById(R.id.item_farm_manager_name);
            headImg = itemView.findViewById(R.id.item_farm_manager_img);
            identImg = itemView.findViewById(R.id.item_farm_manager_img_ident);
        }
    }


    public interface mOnExtraClickListener {

        //删除
        public void onDelClick(View v, int position,String id);

        //取消选中
        public void onUnChecked(int i,String id);

        //选中
        public void onChecked(int i,String id);

    }

}